/**
 * /**
 * Handles the class DBCustomerTest.
 * 
 * Author: Group 3
 * Date: 07-04-2014 Version: 1.0
 */
 
package testlayer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.SQLException;

import dblayer.DBConnection;
import dblayer.DBCities;
import dblayer.DBCustomer;
import modellayer.Cities;
import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import modellayer.Customer;

public class DBCustomerTest {
	
	private static DBCustomer testCustomerDB = new DBCustomer();
	private static Customer testCustomerInsert = new Customer();
	private static DBCities testCitiesDB = new DBCities();
	private static DBConnection testConnection = DBConnection.getInstance();
	
	@BeforeClass
	/**
	 * 
	 * @throws SQLException
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */
	public static void testSetup() throws SQLException, UnsupportedEncodingException, FileNotFoundException{
		
		testConnection.resetDatabase();
		
		testCustomerInsert.setZipCode(6000);
		testCustomerInsert.setAddress("Boligvej 2");
		testCustomerInsert.setCity(testCitiesDB.selectSingleCity(6000));
		testCustomerInsert.setName("Hans");
		testCustomerInsert.setPhoneNo("222222222");
		testCustomerInsert.setCustomerID(7);		
		
	}

	@AfterClass
	public static void testCleanup() throws SQLException, FileNotFoundException {
		
		testConnection.resetDatabase();	
	}

	/**
	 * Tests if you can select all the costumers
	 */
	@Test
	public void testGetAllCustomers() {
		
		assertNotNull(testCustomerDB.getAllCustomers(true).size());
	}

	/**
	 * Tests if you can find a customer based on name
	 */
	@Test
	public void testSearchCustomerOnName() {
		assertNotNull(testCustomerDB.searchCustomerOnName("Sofie", true));
	}

	/**
	 * Tests if you can find a costumer based on customer based on customerID
	 * @throws SQLException
	 */
	@Test
	public void testSelectSingleCustomer() throws SQLException {
		assertNotNull(testCustomerDB.selectSingleCustomer(1, true));
	}

	/**
	 * Tests if you can insert a customer into the database
	 * @throws SQLException
	 */
	@Test
	public void testInsertCustomer() throws SQLException {
		assertNotSame(-1, testCustomerDB.insertCustomer(testCustomerInsert));
	}
	
/**
 * Tests if you can update a customer in the database
 * @throws SQLException
 */
	@Test
	public void testUpdateCustomer() throws SQLException {
		testCustomerDB.selectSingleCustomer(5, true).setPhoneNo("76567582");
		assertNotSame(-1, testCustomerDB.updateCustomer(testCustomerDB.selectSingleCustomer(5, true)));
	}

	/**
	 * Tests if you can delete a customer in the database
	 * @throws SQLException
	 */
	@Test
	public void testDeleteCustomer() throws SQLException {
		
		assertNotSame(-1, testCustomerDB.deleteCustomer(testCustomerDB.selectSingleCustomer(1, true)));
		
	}

}
